package com.hopu.oa.controller.mail;


import com.hopu.oa.dao.mail.AoaMailnumberDao;
import com.hopu.oa.entity.mail.AoaMailnumber;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.mail.AoaMailnumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class MailnumberController {

    private static Integer typet = null;
    private static Integer statust = null;
    private static Integer timet = null;


    @Autowired
    private AoaMailnumberService aoaMailnumberService;

    @Autowired
    private AoaMailnumberDao aoaMailnumberDao;

    @RequestMapping("/accountmanage")
    public String selectAllAoaMail(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size, HttpSession session) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //总数据
        int totalElements = aoaMailnumberService.selectAllAoaMail(userId, baseKey, page, 0, null, null, null).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaMailnumber> list = aoaMailnumberService.selectAllAoaMail(userId, baseKey, (page - 1) * size, size, null, null, 0);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("account", list);
        model.addAttribute("url", "mailDis");
        return "mail/mailmanage";
    }

    @RequestMapping("/mailDis")
    public String sellectMail(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size, HttpSession session, HttpServletRequest request) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        String time = request.getParameter("time");
        String type = request.getParameter("type");
        String status = request.getParameter("status");
        String icon = request.getParameter("icon");
        model.addAttribute("icon", icon);
        if (time != null) {
            timet = Integer.parseInt(time);
            typet = null;
            statust = null;
            model.addAttribute("time", timet);
        }
        if (type != null) {
            timet = null;
            typet = Integer.parseInt(type);
            statust = null;
            model.addAttribute("type", typet);
        }
        if (status != null) {
            timet = null;
            typet = null;
            statust = Integer.parseInt(status);
            model.addAttribute("status", statust);
        }
        //总数据
        int totalElements = aoaMailnumberService.selectAllAoaMail(userId, baseKey, page, 0, null, null, null).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaMailnumber> list = aoaMailnumberService.selectAllAoaMail(userId, baseKey, (page - 1) * size, size, typet, statust, timet);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("account", list);
        model.addAttribute("url", "mailDis");
        return "mail/mailtable";
    }

    @RequestMapping("/addaccount")
    public String addaccount(Model model, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (id != null) {
            Integer mailNumberId = Integer.valueOf(id);
            AoaMailnumber mails = aoaMailnumberDao.selectMailByID(mailNumberId);
            String type = aoaMailnumberDao.getTypeName(mails.getMailType());
            String status = aoaMailnumberDao.getStatusName(mails.getStatus());
            model.addAttribute("status", status);
            model.addAttribute("type", type);
            model.addAttribute("mails", mails);
        }
        List<AoaStatusList> aoaStatusLists = aoaMailnumberDao.getStatus();
        List<AoaTypeList> aoaTypeLists = aoaMailnumberDao.getType();

        model.addAttribute("statuslist", aoaStatusLists);
        model.addAttribute("typelist", aoaTypeLists);
        return "mail/addaccounts";
    }

    @RequestMapping("/saveaccount")
    public String saveaccount(AoaMailnumber aoaMailnumber, HttpSession session, HttpServletRequest request) {

        String page = request.getParameter("page");
        if (page == null) {
            page = "1";
        }
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userId = user.getUserId();

        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        aoaMailnumber.setMailCreateTime(format);
        if (request.getParameter("id") != null) {
            aoaMailnumberDao.updateById(aoaMailnumber);
            return "redirect:/accountmanage?page=" + page;
        } else {
            aoaMailnumber.setMailNumUserId(userId);
            aoaMailnumberDao.insert(aoaMailnumber);
            return "redirect:/accountmanage";
        }
    }

    @RequestMapping("dele")
    public String delMail(@RequestParam("id") int mid, HttpServletRequest request) {
        System.out.println(mid);
        String page = request.getParameter("page");

        aoaMailnumberDao.deleteById(mid);
        return "redirect:/accountmanage?page=" + page;
    }
}
